package com.example.lcl.exp2019_1;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //显示秒表
    private static TextView info;

    //记录工作线程传过来的秒数
    private static int second;
    //UI线程的Handler
    private static Handler mainHandler = new Handler();
    //供工作线程使用的界面更新函数
    public static void UpdateUI(int second){
        MainActivity.second=second;//保存工作线程传过来的数据
        mainHandler.post(RefreshLable);//发送界面更新代码到UI线程上运行
    }
    //实际的界面更新代码
    private static Runnable RefreshLable=new Runnable() {
        @Override
        public void run() {
            info.setText(second);
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        info = (TextView)findViewById(R.id.txtInfo);

        Button start = (Button)findViewById(R.id.btnStart);
        Button add = (Button)findViewById(R.id.btnAdd);
        Button stop = (Button)findViewById(R.id.btnStop);

        final Context context = this;

        //创建用于隐式启动Service的Intent对象
        final Intent intent=new Intent();
        intent.setAction("com.example.lcl.exp2019_1.test");
        intent.setPackage(context.getPackageName());

        //Start Service
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int info_text = Integer.parseInt(info.getText().toString());
                intent.putExtra("data",info_text);
                startService(intent);
            }
        });

        //Add ten Second
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent intent1 = new Intent();
                final int info_text = Integer.parseInt(info.getText().toString());
                intent1.setAction("com.example.lcl.exp2019_1.test");
                intent1.setPackage(context.getPackageName());
                intent1.putExtra("flag",1);
                intent1.putExtra("data",info_text);
                startService(intent1);
            }
        });

        //Stop Service
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intent);
            }
        });

        //创建UI线程的自定义Handler对象，无参构造，直接绑定UI线程的消息循环--利用Message发送界面更新数据
        myMainHandler=new MyMainHandler();
    }

    //处理UI线程Message的自定义Handler对象--static类型，方便工作线程访问
    public static MyMainHandler myMainHandler;
    //消息ID
    public static final int MSGID_1=1;
    //自定义Handler处理UI线程收到的Message
    class MyMainHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case MSGID_1:
                    int val=msg.getData().getInt("data");
                    info.setText("" + val);
                    break;
                default:
                    break;
            }
        }
    }
}
